package org.study.es.boot.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.study.es.boot.entity.User;

import java.util.*;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-12-30
 */
@Slf4j
@Controller
@RequestMapping(value = "/th")
public class ThymeleafController {
    @RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("remark", "后端返回的 index 值");
        return "home/index";
    }

    @RequestMapping("/hello")
    public String hello(ModelMap map) {
        map.addAttribute("remark", "后端返回的 hello 值");
        return "home/hello";
    }

    @RequestMapping("/test")
    public String test(ModelMap map) {
        List<User> userList = new ArrayList<>();

        User u1 = User.builder()
                .username("沉鱼")
                .age(24)
                .password("chenyu")
                .birthday(new Date())
                .desc("<font color='green'><b>talk is cheap, show me the code</b></font>")
                .build();
        userList.add(u1);

        User u2 = User.builder()
                .username("落雁")
                .age(35)
                .password("luoyan")
                .birthday(new Date())
                .desc("<font color='green'><b>talk is cheap, show me the code</b></font>")
                .build();
        userList.add(u2);

        User u3 = User.builder()
                .username("闭月")
                .age(27)
                .password("biyue")
                .birthday(new Date())
                .desc("<font color='green'><b>talk is cheap, show me the code</b></font>")
                .build();
        userList.add(u3);

        User u4 = User.builder()
                .username("羞花")
                .age(27)
                .password("xiuhua")
                .birthday(new Date())
                .desc("<font color='green'><b>talk is cheap, show me the code</b></font>")
                .build();
        userList.add(u4);

        map.addAttribute("user", u1);
        map.addAttribute("userList", userList);
        return "home/test";
    }

    @PostMapping("/save")
    public String saveUser(User user) {
        log.info("调用 saveUser() 方法，请求参数：{}", JSON.toJSONString(user));
        return "redirect:/th/test";
    }

    /**
     * 分页查询信息
     */
    @RequestMapping(value = "/userList", method = {RequestMethod.GET, RequestMethod.POST})
    public String list(Model model, User user, @RequestParam(value = "pageNumber", required = false, defaultValue = "0") Integer pageNumber) {
        int pageSize = 10;
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
        //Page<User> page = userRepository.findAll(Example.of(user, matcher), pageRequest);
        //Page<User> page = Page.empty(pageRequest);
        Page<User> page = new PageImpl(Lists.newArrayList(user));
        // 分页查询数据
        model.addAttribute("page", page);
        //查询条件
        model.addAttribute("user", user);
        //页面标题
        model.addAttribute("title", "用户管理");
        // 转到待渲染模板，所有模板都在 templates 文件夹下，home/list 指 templates 文件夹下的 home 文件夹下的 user-list.html 页面
        return "home/user-list";
    }
}
